package com.chj.leetcode.tijie900to950;

public class Code917_ReverseOnlyLetters2 {

	public static void reverseString(char[] s) {
		int left = 0;
		int right = s.length - 1;
		reverse(s, left, right);
	}

	public static void reverse(char[] chs, int l, int r) {
		while (l < r) {
			char temp = chs[l];
			chs[l++] = chs[r];
			chs[r--] = temp;
		}
	}

	public static boolean is0To9(char s) {
		if (s >= '0' && s <= '9') {
			return true;
		}

		return false;
	}

	public static String reverse_vowel(String str) {

		char[] cs = str.toCharArray();
		int n = cs.length;

		char[] numbers = new char[n];
		int numIndex = 0;
		for (int i = 0; i < n; i++) {
//			if (Character.isDigit(cs[i])) {
//				numbers[numIndex++] = cs[i];
//			}

			if (is0To9(cs[i])) {
				numbers[numIndex++] = cs[i];
			}
		}
		numIndex--;

		for (int i = 0; i < n; i++) {
//			if (Character.isDigit(cs[i])) {
//				cs[i] = numbers[numIndex--];
//			}

			if (is0To9(cs[i])) {
				cs[i] = numbers[numIndex--];
			}
		}

		return String.valueOf(cs);
	}

	public static String reverse_vowel02(String str) {
		char[] cs = str.toCharArray();
		char[] rcs = str.toCharArray();

		reverseString(rcs);
		int len = cs.length;
		int j = 0;
		for (int i = 0; i < len; i++) {
			if (is0To9(cs[i])) {
				while (!is0To9(rcs[j])) {
					j++;
				}
				cs[i] = rcs[j++];
			}
		}

		return String.valueOf(cs);
	}

	public static void main(String[] args) {
		String str = "a132bcd456efg";
		System.out.println(reverse_vowel(str));
		System.out.println(reverse_vowel02(str));
//		a654bcd231efg
	}

}
